
<!--用户信息界面-->

<template>
  <el-card style="width: 500px;">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="">
        <el-upload
            class="avatar-uploader"
            :action="'http://'+serverIp()+':8081/jdyfy/file/upload'"
            :show-file-list="true"
            :on-success="handleAvatarSuccess">
          <img v-if="form.avatar" :src="form.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="form.username" disabled ></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="所属科室/企业">
        <el-input v-model="form.department"></el-input>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model="form.phoneNumber"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="form.roleId" placeholder="请选择角色" disabled>
          <el-option
                v-for="item in roleOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="联系地址">
        <el-input v-model="form.address"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">确定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import {addUser, getUserById, getUserByUsername} from "@/api/user";
import {serverIp} from "../../../../public/config.js";

export default {
  name: 'userinfo',
  data() {
    return {
      user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {},
      form: {
        username: '',
        name: '',
        phoneNumber: '',
        roleId: '',
        email: '',
        address: '',
        avatar:''
      },
      roleOptions: [
          {label: '管理员',value: 1 },
          {label: '项目负责人',value: 2},
          {label: '供应商', value: 3},
          {label: '主管',value: 4 }
      ],

    }
  },
  created() {
    this.init().then(res => {
      this.form = res
    })
  },
  methods: {
    serverIp() {
      return serverIp
    },
    save() {
      addUser(this.form).then(res => {
        if(res) {
          this.$message.success('保存用户信息成功')

          // 刷新主页面的方法以更新用户信息
          this.$emit("refreshUser")

          this.init().then(res => {
            res.token = JSON.parse(localStorage.getItem('user')).token
            localStorage.setItem('user', JSON.stringify(res))
          })

        } else {
          this.$message.error('保存用户信息失败')
        }
      });
    },
    async init() {
      return await getUserByUsername(this.user.username)
    },
    handleAvatarSuccess(res) {
      this.form.avatar = res
    }
  },

}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 32px;
  color: #8c939d;
  width: 188px;
  height: 188px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>